#include "vxcrosshair.h"


VxCrosshair::VxCrosshair(QWidget * parent) :
		QWidget::QWidget(parent), _mpoint(new QPoint(0,0)), _excludes(new QList<QWidget *>())
{
	this->setAttribute(Qt::WA_TransparentForMouseEvents);

	qApp->installEventFilter(this);

	QCursor * cursor = new QCursor(QPixmap(":/images/Cursors/crissh1.png"), 6, 6);
	setCursor(*cursor);
	_hline = new QWidget(this);
	_vline = new QWidget(this);

	_hline->setCursor(*cursor);
	_vline->setCursor(*cursor);

	_hline->setAttribute(Qt::WA_TransparentForMouseEvents);
	_vline->setAttribute(Qt::WA_TransparentForMouseEvents);

	_hline->setAutoFillBackground(true);
	_vline->setAutoFillBackground(true);

	QPalette palette = _hline->palette();
	palette.setColor(_hline->backgroundRole(), QColor(0xdf, 0xe0, 0xf1, 100));

	_hline->setPalette(palette);
	_vline->setPalette(palette);

	//
	_hbline = new QWidget(_hline);
	_vbline = new QWidget(_vline);
	_hbline->setAutoFillBackground(true);
	_vbline->setAutoFillBackground(true);

	palette = _hbline->palette();

	palette.setColor(_hbline->backgroundRole(), QColor(0x98, 0x98, 0xce, 100));

	_hbline->setPalette(palette);
	_vbline->setPalette(palette);

	_hbline->setFixedSize(13,1);
	_vbline->setFixedSize(1,13);
}




VxCrosshair::~VxCrosshair()
{
	delete _excludes;
}


bool VxCrosshair::eventFilter(QObject * obj, QEvent * event)
{
	if (event->type() == QEvent::Resize) {
		QRegion cr = parentWidget()->contentsRect();
		QRect br = cr.boundingRect();
		setGeometry(0,0, br.width(), br.height());

		_hline->setFixedHeight(1);
		_hline->setFixedWidth(br.width());
		_vline->setFixedWidth(1);
		_vline->setFixedHeight(br.height());

	} else
	if (event->type() == QEvent::MouseMove) {
		QMouseEvent * mevent = static_cast<QMouseEvent *>(event);
		QPoint p = parentWidget()->mapFromGlobal(mevent->globalPos());

		if (_validateXY(&p)){


			if (_hline->isHidden()){
				_hline->show();
				_vline->show();
			}

			setMask(parentWidget()->contentsRect());
			_hline->move(0, p.y());
			_vline->move(p.x(), 0);
			_hbline->move(p.x()-6, 0);
			_vbline->move(0, p.y()-6);
		} else {
			if (_hline->isVisible()){
				_hline->hide();
				_vline->hide();
			}
		}

	}
	return QObject::eventFilter(obj, event);
}


bool VxCrosshair::_validateXY(QPoint * p)
{

	foreach(QObject * object, *_excludes){
		QWidget * widget = dynamic_cast<QWidget *>(object);

		if (widget->geometry().contains(*p)){
			return false;
		}
	}

	return true;
}


void VxCrosshair::paintEvent(QPaintEvent *event)
{
//	QWidget::paintEvent(event);
/*	QPainter * painter = new QPainter(this);
	painter->setPen(QPen(QColor(0xdf, 0xe0, 0xf1)));

	int xx = _mpoint->x();
	int yy = _mpoint->y();

	QList<int> * xcoords = new QList<int>();
	QList<int> * ycoords = new QList<int>();

	xcoords->append(0);
	xcoords->append(width());

	ycoords->append(0);
	ycoords->append(height());

	foreach(QObject * object, *_excludes){
		QWidget * widget = dynamic_cast<QWidget *>(object);

		QPoint tl = this->mapFromGlobal(widget->mapToGlobal(QPoint(0,0)));
		QPoint br = this->mapFromGlobal(widget->mapToGlobal(QPoint(widget->width(), widget->height())));

		if (yy > tl.y() && yy < br.y()){
			xcoords->append(tl.x());
			xcoords->append(br.x());
		}
		if (xx > tl.x() && xx < br.x()){
			ycoords->append(tl.y());
			ycoords->append(br.y());
		}
	}


	qSort(*xcoords);

	for (int i=0; i < xcoords->size(); i++){
		if (i%2 == 1){
			continue;
		}
		int x0 = xcoords->at(i);
		int x1 = xcoords->at(i+1);
		if (x1 - x0 < 5){
			continue;
		}
		painter->drawLine(x0+1, yy, x1-2, yy);
	}

	qSort(*ycoords);
	for (int i=0; i < ycoords->size(); i++){
		if (i%2 == 1){
			continue;
		}
		int y0 = ycoords->at(i);
		int y1 = ycoords->at(i+1);
		if (y1 - y0 < 5){
			continue;
		}
		painter->drawLine( xx, y0+1, xx, y1-2);

	}

	painter->setPen(QPen(QColor(0xca, 0xca, 0xe5)));
	painter->drawLine( xx, yy+7, xx, yy-8);
	painter->drawLine( xx+7, yy, xx-8, yy);

	painter->setPen(QPen(QColor(0xac, 0xac, 0xd7)));
	painter->drawLine( xx, yy+4, xx, yy-5);
	painter->drawLine( xx+4, yy, xx-5, yy);

	painter->setPen(QPen(QColor(0x98, 0x98, 0xce)));
	painter->drawLine( xx, yy+2, xx, yy-3);
	painter->drawLine( xx+2, yy, xx-3, yy);


	delete xcoords;
	delete ycoords;

	delete painter;
*/
}


void VxCrosshair::exclude(QWidget * widget)
{
	_excludes->append(widget);
}

